<?php

namespace Unicom\Controller;

use Think\Controller;

class CuccMemberController extends AdminController
{

    public function list_()
    {
        layout(false);
        $model = M('wa.member_order', 'cucc_');
        $REQUEST = (array)I('request.');
        $product_type = I('param.product_type', 0);

        $search = $REQUEST["search"];
        import('ORG.Util.Page'); // 导入分页类
        if (!empty($search)) {
            $where = "where oid like '$search%' or cal like '$search%' or channel=$search";
        }

        $count = $model->query("select count(*) as total_count,FROM_UNIXTIME(`timestamp`, '%Y-%m-%d') as today,channel_id from wa.cucc_member_order 
          group by today,product_code ")[0]['total_count']; // 查询满足要求的总记录数

        $listRows = 10;
        if (isset($REQUEST['r'])) {
            $listRows = (int)$REQUEST['r'];
        } else {
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        }
        $Page = new \Think\Page($count, $listRows); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出

        //$data = $model->query("call wa.cuccList(" . $Page->firstRow . "," . $listRows . ")");
        $page = $Page->firstRow;
        $sql = "select FROM_UNIXTIME(`timestamp`, '%Y-%m-%d') as today,channel_id as channel,count(*) as count,product_code, 
        (select count(*) from wa.cucc_member_order where FROM_UNIXTIME(`timestamp`, '%Y-%m-%d')=today and state=1 and product_code=a.product_code ) as success_count
        from wa.cucc_member_order as a

        group by today,product_code 
        order by `timestamp` desc
        limit $page,$listRows";
        $data = $model->query($sql);


        $this->assign('data', $data);

        $this->assign('page', $show);
        //	var_dump($Page, $count);
        $this->display();

    }



    public function list1_()
    {
        layout(false);
        $model = M('wa.member_order', 'cucc_');
        $start=I('param.start',date('Y-m-d'));
        $end=I('param.end',date('Y-m-d'));


        $sql = "select FROM_UNIXTIME(a.create_at, '%Y-%m-%d') as today,channel,product as fee,count(*) as up_count,
    (select count(*) from wa.cucc_member_verify where FROM_UNIXTIME(create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and channel=a.channel and product=a.product and vcode!='' ) as code_count,
    (select count(*) from wa.cucc_member_verify where FROM_UNIXTIME(create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and channel=a.channel and product=a.product and `status`=1 ) as success_count,
    (select count(*) from wa.cucc_member_verify where FROM_UNIXTIME(create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and channel=a.channel and product=a.product and sync=1 ) as sync_count
    
    from wa.cucc_member_verify as a
    where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN '$start' and '$end'
    group by today,channel,product
    ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->assign('start',$start);
        $this->assign('end',$end);


        $this->display();

    }


    public function find_()
    {
        layout(false);
        $model = M('wa.month', 'cp_');
        $today = I('param.today');
        $channel = I('param.channel');
        $phone=I('param.phone');


        $sql = "
        select a.*,channel_id as channel,FROM_UNIXTIME(timestamp, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence
        from wa.cucc_member_order as a
        left join cp.cp_privence as p on left (a.mobile,7)=p.dcmobile
        where mobile='$phone'
        
        order by a.create_at desc 
        ";
        $data = $model->query($sql);

        $this->assign("phone",$phone);
        $this->assign('data', $data);
        $this->display();

    }


    //成功详情

    public function info_()
    {
        layout(false);
        $model = M('wa.month', 'cp_');
        $today = I('param.today');
        $channel = I('param.channel');

        $sql = "
        select a.*,channel_id as channel,FROM_UNIXTIME(timestamp, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence
        from wa.cucc_member_order as a
        left join cp.cp_privence as p on left (a.mobile,7)=p.dcmobile
        where FROM_UNIXTIME(timestamp, '%Y-%m-%d')='$today' and product_code='$channel'
        
        order by a.create_at desc 
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display();

    }


    public function info1_(){
        layout(false);
        $model = M('wa.month', 'cp_');

        $channel = I('param.channel');
        $start=I('param.start',date('Y-m-d'));
        $end=I('param.end',date('Y-m-d'));
        $fee=I('param.fee');
        $tag =I('param.tag');


        $sql = "
        select a.*,p.dcprivence,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at from wa.cucc_member_verify as a
         left join cp.cp_privence as p on left (a.phone,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and channel='$channel' and product='$fee' and a.status=1
        order by a.create_at desc 
        ";
        switch ($tag){
            case "1":
                $sql = "
        select a.*,p.dcprivence,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at from wa.cucc_member_verify as a
         left join cp.cp_privence as p on left (a.phone,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and channel='$channel' and product='$fee' 
        order by a.create_at desc 
        ";
                break;
            case "2":
                $sql = "
        select a.*,p.dcprivence,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at from wa.cucc_member_verify as a
         left join cp.cp_privence as p on left (a.phone,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and channel='$channel' and product='$fee' and a.status=1
        order by a.create_at desc 
        ";
                break;
            case "3":
                $sql = "
        select a.*,p.dcprivence,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at from wa.cucc_member_verify as a
         left join cp.cp_privence as p on left (a.phone,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and channel='$channel' and product='$fee' and sync=1
        order by a.create_at desc 
        ";
                break;
            case "4":
                $sql = "
        select a.*,p.dcprivence,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at from wa.cucc_member_verify as a
         left join cp.cp_privence as p on left (a.phone,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and channel='$channel' and product='$fee' and a.vcode!='' 
        order by a.create_at desc 
        ";
                break;


        }
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display("info1_");

    }


    //成功详情

    public function successinfo_()
    {
        layout(false);
        $model = M('wa.month', 'cp_');

        $channel = I('param.channel');
        $start=I('param.start',date('Y-m-d'));
        $end=I('param.end',date('Y-m-d'));
        $fee=I('param.fee');

        $sql = "
        select a.*,p.dcprivence from wa.cucc_member_verify
         left join cp.cp_privence as p on left (a.phone,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN '$start' and '$end' and product='$fee' and state=1
        
        order by a.create_at desc 
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display("info_");

    }

    public function privence_()
    {


        layout(false);
        $model = M('wa.month', 'cp_');
        $date = I('param.date',date('Y-m-d'));
        $tag = I('param.tag',0);
        $time_format=$tag==0?'%Y-%m-%d':'%Y-%m';

        $sql = "
        select channel_id as channel,p.dcprivence,count(*) count
        from wa.cucc_member_order as a
        left join cp.cp_privence as p on left (a.mobile,7)=p.dcmobile
        where FROM_UNIXTIME(timestamp, '$time_format')='$date' 
        group by p.dcprivence,product_code
        order by a.create_at desc
        ";
        $data = $model->query($sql);

        $this->assign('date', $date);
        $this->assign('data', $data);
        $this->display();

    }
}